package com.zmc.admin.controller.common;

import com.zmc.admin.application.CommonService;
import com.zmc.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 通用请求处理
 * @author zmc
 */
@RestController
@RequestMapping("/common")
public class CommonController {


    @Autowired
    private CommonService commonService;

    /**
     * 通用下载请求
     *
     * @param fileName 文件名称
     * @param delete   是否删除
     */
    @GetMapping("/download")
    public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
        commonService.fileDownload(fileName, delete, response, request);
    }

    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/upload")
    public R uploadFile(MultipartFile file) throws Exception {
        return commonService.uploadFile(file);
    }

    /**
     * 通用上传请求（多个）
     */
    @PostMapping("/uploads")
    public R uploadFiles(List<MultipartFile> files) throws Exception {
        return commonService.uploadFiles(files);
    }

    /**
     * 本地资源通用下载
     */
    @GetMapping("/download/resource")
    public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        commonService.resourceDownload(resource,request, response);
    }
}
